package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/list")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @GetMapping("/createIndex")
    public Result createIndex(){
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * 上架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable(value = "skuId") Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable(value = "skuId") Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    /**
     * 更新商品incrHotScore
     *
     * @param skuId
     * @return
     */
    @GetMapping("/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable("skuId") Long skuId) {
        // 调用服务层
        searchService.incrHotScore(skuId);
        return Result.ok();
    }

    /**
     * 搜索实现
     * @param searchParam
     * @return
     */
    @PostMapping(value = "/search")
    public SearchResponseVo search(SearchParam searchParam,
                                   @RequestParam Map<String,String> searchMap){
        SearchResponseVo search = searchService.search(searchParam,searchMap);
        return search;
    }
}
